<?php
/**
 * Created by PhpStorm.
 * User: sxg
 * Date: 2023-07-16
 * Time: 11:56
 */

namespace App\Controller;

class WsController extends WsBaseController {

    /**
     * gpio引脚控制led
     * @param $frame
     * @return string
     */
    public function gpio($frame) : string {
        $frame['d'] = 'led';
        if ($this->publishEsp32Data("esp32", json_encode($frame, JSON_UNESCAPED_UNICODE))) {
            $ret = json_encode(['msg' => 'already send data to esp32'], JSON_UNESCAPED_UNICODE);
        } else {
            $ret = json_encode(['msg' => 'esp32 device not found'], JSON_UNESCAPED_UNICODE);
        }
        return $ret;
    }

    /**
     * 转发数据给esp32显示在oled屏幕上
     * @param $frame
     * @return string
     */
    public function oled($frame) : string {
        $frame['d'] = 'oled';
        if ($this->publishEsp32Data("esp32", json_encode($frame, JSON_UNESCAPED_UNICODE))) {
            $ret = json_encode(['msg' => 'already send data to esp32'], JSON_UNESCAPED_UNICODE);
        } else {
            $ret = json_encode(['msg' => 'esp32 device not found'], JSON_UNESCAPED_UNICODE);
        }
        return $ret;
    }


    /**
     * 模拟数字转换
     * @param $frame
     * @return string
     */
    public function adc($frame) : string {
        $frame['d'] = 'adc';
        if ($this->publishEsp32Data("esp32", json_encode($frame, JSON_UNESCAPED_UNICODE))) {
            $ret = json_encode(['msg' => 'already send data to esp32'], JSON_UNESCAPED_UNICODE);
        } else {
            $ret = json_encode(['msg' => 'esp32 device not found'], JSON_UNESCAPED_UNICODE);
        }
        return $ret;
    }


    /**
     * websocket订阅【接收esp发过来的温湿度】
     * @param $frame
     * @return string
     */
    public function mqtt($frame): string {
        $this->client->subscribeTopic = $frame['TopicName'];
        print_r("客户端连接对象的订阅主题是:".$this->client->subscribeTopic."\r\n");
        return json_encode(['msg' => '主题订阅成功'], JSON_UNESCAPED_UNICODE);
    }
}